import {Component} from '@angular/core';
import {NavController, NavParams} from 'ionic-angular';
import {HttpService} from '../../../providers/HttpService'
import {NativeServiceC} from '../../../providers/NativeServiceC'

@Component({
    selector: 'page-classify-detail',
    templateUrl: 'classifyDetail.html'
})
export class ClassifyDetailPage {

    title:string;
    typeId:number;
    type:string;
    items:Object[];

    constructor(public navCtrl:NavController,
                public navParams:NavParams,
                public httpService:HttpService,
                public nativeService:NativeServiceC) {
        this.title = navParams.get('title');
        this.typeId = navParams.get('id');
        this.type = navParams.get('type');
    }

    ionViewWillEnter() {
    }

    ionViewDidLoad() {
        this.getItems();
    }

    homeCellClick(index){
        console.log('cell组件通信',index);
    }
    homeLikeClick(index){
        console.log('like组件通信',index);
    }

    private getItems() {
        let url,key;
        if (this.typeId === 0){
            url = '/v1/collections';
            key = 'collections'
        }else if (this.type === 'collections'){
            url = '/v1/collections/' + this.typeId +'/posts';
            key = 'posts'
        }else {
            url = '/v1/channels/' + this.typeId +'/items';
            key = 'items'
        }
        let sender = {
                limit: 20,
                offset: 0
            };
        this.httpService.get(url, sender).subscribe(res => {
            this.nativeService.hideLoading();
            this.items = res[key];
        })
    }

}
